﻿using System;
using System.Linq;
using System.Web.Mvc;
using Willow.IService;
using Willow.IService.Auth;
using Willow.Model;
using Willow.MVC.Result;
namespace Willow.WebApplication.Areas.Auth.Controllers
{
    public class ModuleController : Controller
    {

        #region 属性

        public IModuleService ModuleService { get; set; }
        public IModuleActionService ModuleActionService { get; set; }

        #endregion


        #region 页面

        /// <summary>
        /// 模块管理页
        /// </summary>
        /// <returns></returns>
        public ViewResult Management()
        {
            return View();
        }

        #endregion


        #region 操作

        /// <summary>
        /// 为树获取所有模块
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllModuleForTree()
        {
            var data = ModuleService.Filter(p => p.IsDelete == false)
                .OrderBy(p => p.DisplayOrder)
                .Select(p => new { id = p.Id, text = p.Name, parentId = p.ParentId, iconCls = "", attributes = new { type = "module" } })
                .ToList();
            var lstAction = ModuleActionService.Filter(p => p.Auth_Action.IsDelete == false)
                .OrderBy(p => p.Auth_Action.DisplayOrder)
                .Select(p => new { id = p.Auth_Action.Id, text = p.Auth_Action.Name, parentId = p.ModuleId, iconCls = p.Auth_Action.Icon, attributes = new { type = "action" } })
                .ToList();
            data.AddRange(lstAction);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 为树列表获取所有模块
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public JsonResult GetAllModuleForTreeGrid(string name = "")
        {
            var data = ModuleService
                .Query(p => p.IsDelete == false && p.Name.Contains(name))
                .OrderBy(p => p.DisplayOrder)
                .Select(p => new
                {
                    p.Id,
                    p.ParentId,
                    p.FullParentId,
                    p.Name,
                    p.DisplayOrder,
                    p.URL,
                    p.IsDelete,
                    p.Icon
                }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存模块
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        public JsonResult Save(Auth_Module module, SaveActionEnum action)
        {
            return this.StandardJson(() =>
            {
                ModuleService.Save(module, action);
            });
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public JsonResult Delete(Guid id)
        {
            return this.StandardJson(() =>
            {
                ModuleService.LogicalDelete(id);
            });
        }

        /// <summary>
        /// 分配模块和功能
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <param name="moduleIds">The module ids.</param>
        /// <param name="actionIds">The action ids.</param>
        /// <returns></returns>
        public JsonResult DistributeModuleAndAction(Guid roleId, Guid[] moduleIds, Guid[] actionIds)
        {
            return this.StandardJson(() =>
            {
                ModuleService.DistributeModuleAndAction(roleId, moduleIds, actionIds);
            });
        }

        #endregion

    }
}